<?php
declare(strict_types=1);

/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 14:05
 */

namespace App\Controller;

use App\Libs\Common;
use App\Libs\Extra\Luosimao;
use App\Libs\ReMsg;
use App\Libs\DiyError;
use App\Libs\Upload;
use App\Model\IntegralLog;
use App\Model\Party;
use App\Model\User;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Contract\SessionInterface;
use Hyperf\Redis\Redis;

class AppBaseController extends AbstractController
{
    #[Inject] protected SessionInterface $session;
    #[Inject] protected Redis $redis;
    #[Inject] protected Luosimao $luosimao;
    #[Inject] protected ReMsg $reMsg;
    #[Inject] protected DiyError $diyError;
    #[Inject] protected Common $common;
    #[Inject] protected Upload $upload;

    protected string|null $redisPrefix;
    protected string|null $app_env;
    protected string|null $upload_path;

    protected function __construct()
    {
        date_default_timezone_set('Asia/ShangHai');
        $this->redisPrefix = '000'.config('app_name').'-'.config('app_env');
        $this->app_env = config('app_name').'-'.config('app_env');
        $this->upload_path = config('upload')['path'];
    }


    /**
     * 验证用户信息
     * @return array
     */
    protected function checkUser():array
    {
        //获取用户信息
        $checkData = $this->getUserSession();
        if(empty($checkData)
            || !isset($checkData->id) || $checkData->id==null
            || !isset($checkData->user_type) || $checkData->user_type==null
            || !isset($checkData->party_id) || $checkData->party_id==null
        ){
            //根据openid获取用户信息
            $userInfo = User::selectRaw('id,user_type,party_id')->where(['openid' => $checkData->openid])->first();
            if(empty($userInfo)) return ['status'=>40002,'message'=>'需要登录','data'=>null];
            $checkData->id = $userInfo->id;
            $checkData->user_type = $userInfo->user_type;
            $checkData->party_id = $userInfo->party_id;
            $this->session->set('api_user',$checkData);
            $this->session->save();
        }
        return ['status'=>40001,'message'=>'success','data'=>$checkData];
    }

    /**
     * 获取用户session信息
     * @return object|null
     */
    protected function getUserSession():object|null
    {
        return $this->session->get('api_user',null);
    }



    /**
     * 验证管理员信息
     * @return array
     */
    public function checkAdmin() :array
    {
        //获取管理员信息
        $checkData = $this->getAdminSession();
        if(empty($checkData)
            || !isset($checkData->id) || $checkData->id==null
            || !isset($checkData->username) || $checkData->username==null
            || !isset($checkData->level) || $checkData->level==null
        ){
            $this->session->set('api_admin',null);
            $this->session->save();
            return ['status'=>40002,'message'=>'需要登录','data'=>null];
        }
        return ['status'=>40001,'message'=>'success','data'=>$checkData];
    }


    /**
     * 获取用户session信息
     * @return object|null
     */
    protected function getAdminSession():object|null
    {
        return $this->session->get('api_admin',null);
    }


    /**
     * 制作一个redis锁
     * @param string $key
     * @param string $value
     * @return bool
     * @throws \RedisException
     */
    protected function lock(string $key, string $value) :bool
    {
        return $this->redis->setnx($key, $value);
    }


    /**
     * 积分日志
     * @param array $params
     * @return bool
     */
    protected function makeIntegralLog(array $params): bool
    {
        if(empty($params)) return false;
        if(!isset($params['type']) || !in_array($params['type'],[1,2,4,5,6,7,8,9,10])) return false;
        if(!isset($params['action']) || !in_array($params['action'],[1,2])) return false;
        if(!isset($params['uid']) || $params['uid']==null) return false;
        (isset($params['party_id']) && $params['party_id']) ? $party_id = $params['party_id'] : $party_id=null;

        $type = $params['type'];
        $action = $params['action'];
        $uid = $params['uid'];

        //$type==3 后台  支部活动 审核加积分
        switch ($type){
            case 1:
                $action_tip = '学习';
                $score = 1;
                break;
            case 2:
                $action_tip = '评论';
                $score = 1;
                break;
            case 4:
                $action_tip = '支部活动点赞';
                $score = 1;
                break;
            case 5:
                $action_tip = '活动答题';
                $score = 5;
                break;
            case 6:
                $action_tip = '活动报名';
                $score = 5;
                break;
            case 7:
                $action_tip = '活动抽奖';
                $score = 5;
                break;
            case 8:
                $action_tip = '活动打卡';
                $score = 5;
                break;
            case 9:
                $action_tip = '活动问卷';
                $score = 5;
                break;
            case 10:
                $action_tip = '支部信息更新';
                $score = 1;
                break;
            default:
                $score = 0;
                $action_tip = null;
                break;
        }

        //积分记录
        $data['type'] = $type;
        $data['action'] = $action;
        $data['action_tip'] = $action_tip;
        $data['score'] = $score;
        $data['uid'] = $uid;
        $data['party_id'] = $party_id;
        $addRet = IntegralLog::create($data);
        if($addRet){
            //操作用户积分
            if(in_array($type,[1,2,5,6,7,8,9])){
                $userInfo = User::where('id',$uid)->first();
                if($action==1){
                    //加积分
                    $userInfo->integral += $score;//加积分
                }else{
                    //减去积分
                    $userInfo->integral -= $score;//减积分
                }
                $userInfo->integral_time = date('Y-m-d H:i:s');
                $userInfo->save();
            }

            //操作支部积分
            if($party_id && in_array($type,[4,10])){
                $partyInfo = Party::where('id',$party_id)->first();
                if($action==1){
                    $partyInfo->integral += $score;//加积分
                }else{
                    $partyInfo->integral -= $score;//减积分
                }
                $partyInfo->integral_time = date('Y-m-d H:i:s');
                $partyInfo->save();
            }
            return true;
        }
        return false;
    }



    /**
     * 后台-积分日志
     * @param array $params
     * @return bool
     */
    protected function makeAdminIntegralLog(array $params): bool
    {
        if(empty($params)) return false;
        if(!isset($params['type']) || $params['type'] != 3) return false;
        if(!isset($params['action']) || !in_array($params['action'],[1,2])) return false;
        if(!isset($params['party_id']) || $params['party_id']==null) return false;

        $type = $params['type'];
        $action = $params['action'];
        $party_id = $params['party_id'];

        switch ($type){
            case 3:
                $action_tip = '支部活动';
                $score = 5;
                break;
            default:
                $score = 0;
                $action_tip = null;
                break;
        }
        //积分记录
        $data['type'] = $type;
        $data['action'] = $action;
        $data['action_tip'] = $action_tip;
        $data['score'] = $score;
        $data['party_id'] = $party_id;
        $addRet = IntegralLog::create($data);
        if($addRet){
            //操作支部积分
            $partyInfo = Party::where('id',$party_id)->first();
            if($action==1){
                $partyInfo->integral_active += $score;//加积分
            }else{
                $partyInfo->integral_active -= $score;//减积分
            }
            $partyInfo->integral_time = date('Y-m-d H:i:s');
            $partyInfo->save();
            return true;
        }
        return false;
    }









}